import axios from "axios";
import ElementUI from 'element-ui';
import store from './store'
import router from './router'

//方便管理请求,方便修改请求链接的域名
axios.defaults.baseURL="http://localhost:8081";

//前置拦截
axios.interceptors.request.use(config =>{

    return config
})

//后置拦截
axios.interceptors.response.use(response => {
    let res = response.data;

    console.log("==============")
    console.log(res)
    console.log("==============")

    if (res.code == 200) {
        return response
    } else {
        ElementUI.Message.error(response.data.msg, {duration : 3*1000});

        //不让进入Login.vue
        return Promise.reject(response.data.msg)
    }
},
    error => {
    console.log(error)
        if (error.response.data){
            error.message = error.response.data.msg
        }
        if (error.response.status === 401){
            store.commit("REMOVE_INFO")
            router.push("/login")
        }
        ElementUI.Message.error(error.message, {duration : 3*1000});

        return Promise.reject(error)
}
)